
package com.sun.showcase.biz.testcase.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.testcase.dao.TestTaskDao;
import com.sun.showcase.biz.testcase.service.TaskCaseService;
import com.sun.showcase.biz.testcase.service.TestTaskService;
import com.sun.showcase.client.testcase.domain.TestTask;
import com.sun.showcase.client.testcase.query.TaskCaseQuery;
import com.sun.showcase.client.testcase.query.TestTaskQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("testTaskService")
@Transactional
public class TestTaskServiceImpl implements TestTaskService{
	@Autowired
	private TestTaskDao testTaskDao;
	@Autowired
	private TaskCaseService taskCaseService;
	
	public void setTestTaskDao(TestTaskDao dao) {
		this.testTaskDao = dao;
	}

	public DataGrid datagrid(TestTaskQuery testTaskQuery) {
		DataGrid j = new DataGrid();
		Pager<TestTask> pager  = find(testTaskQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<TestTaskQuery> getQuerysFromEntitys(List<TestTask> testTasks) {
		List<TestTaskQuery> testTaskQuerys = new ArrayList<TestTaskQuery>();
		if (testTasks != null && testTasks.size() > 0) {
			for (TestTask tb : testTasks) {
				TestTaskQuery b = new TestTaskQuery();
				BeanUtils.copyProperties(tb, b);
				testTaskQuerys.add(b);
			}
		}
		return testTaskQuerys;
	}

	private Pager<TestTask> find(TestTaskQuery testTaskQuery) {
		Page<TestTask> page = PageHelper.startPage(testTaskQuery.getPage().intValue(), testTaskQuery.getRows().intValue());
		List<TestTask> list = testTaskDao.findList(testTaskQuery);
		Pager<TestTask> pager = new Pager<TestTask>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public TestTask add(TestTaskQuery testTaskQuery) {
		TestTask t = new TestTask();
		BeanUtils.copyProperties(testTaskQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
	    t.setBlockCount(0);
	    t.setNgCount(0);
	    t.setOkCount(0);
		testTaskDao.save(t);
		testTaskQuery.setId(t.getId());
		return t;
		
	}

	public void update(TestTaskQuery testTaskQuery) {
		TestTask t = testTaskDao.getById(testTaskQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(testTaskQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	testTaskDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(TestTaskQuery testTaskQuery) {
	    testTaskDao.updatePart(testTaskQuery);
	}

	public void delete(java.lang.Long[] ids) {
		testTaskDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Long[] ids) {
		testTaskDao.deletePt(ids);
	}

	public TestTask get(TestTaskQuery testTaskQuery) {
		return testTaskDao.getById(testTaskQuery.getId());
	}

	public TestTask get(Long id) {
		return testTaskDao.getById((id));
	}

	
	public List<TestTaskQuery> listAll(TestTaskQuery testTaskQuery) {
	    List<TestTask> list = testTaskDao.findList(testTaskQuery);
		List<TestTaskQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void getAllCount(TestTaskQuery testTaskQuery) {
		TaskCaseQuery tmp = new TaskCaseQuery();
		tmp.setTaskGroupId(testTaskQuery.getTaskGroupId());
		tmp.setTaskId(testTaskQuery.getId());
		tmp.setStatus(StringConstant.ZERO);
		testTaskQuery.setTodoCount(taskCaseService.getStatusCount(tmp));
		tmp.setStatus(StringConstant.ONE);
		testTaskQuery.setOkCount(taskCaseService.getStatusCount(tmp));
		tmp.setStatus(StringConstant.TWO);
		testTaskQuery.setNgCount(taskCaseService.getStatusCount(tmp));
		tmp.setStatus(StringConstant.THREE);
		testTaskQuery.setBlockCount(taskCaseService.getStatusCount(tmp));
		tmp.setStatus(StringConstant.FOUR);
		testTaskQuery.setNgToOkCount(taskCaseService.getStatusCount(tmp));
	}
	
	
}
